#include<iostream>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
vector<int>ans;
const int N = 55;
//string s[N];
char s[N][N];
int n, z;
int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}
bool fun(int k)
{
	ans.clear();
	for (int i = 0; i < n; i += k) {
		for (int j = 0; j < n; j += k) {
			int k1 = s[i][j] - 'a' + 1;
			ans.push_back(k1);
			//cout<<"k1=="<<k1<<endl;
			for (int z1 = i; z1 < i + k; z1++) {
				for (int z2 = j; z2 < j + k; z2++) {
					if (z1 == i && z2 == j)continue;
					int k2 = s[z1][z2] - 'a' + 1;
					if (k2 != k1) {
						return 0;
					}
				}
			}
		}
	}
	return 1;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		ans.clear();
		scanf("%d%d", &n, &z);
		int n1 = n;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> s[i][j];
			}
		}
		if (n * z % 100 != 0)printf("error\n");
		else {
			//cout<<"1==="<<endl;
			bool flag = 0;
			int r = 0;
			if (z == 125)if (!fun(4))flag = 1; else r = 4;
			else if (z == 150)if (!fun(2))flag = 1; else r = 2;
			else if (z == 175)if (!fun(4))flag = 1; else r = 4;
			else if (z == 100 || z == 200)if (!fun(1))flag = 1; else r = 1;
			if (flag) {
				printf("error\n");
			}
			else {
				//cout<<"111"<<endl;
				int z1 = n * z / 100;
				//cout<<"ans=="<<(double)(z1*1.0/n)<<endl;
				int z2 = gcd(z1, n);
				z1 /= z2;
				n /= z2;
				//cout<<"r=="<<r<<endl;
//				cout<<"z1=="<<z1<<endl;	
				//cout<<"r=="<<r<<endl;
				//cout<<"n=="<<n<<endl;
				int cnt = 0;
				//				for(auto x:ans){
				//					cout<<"x=="<<x<<endl;
				//				}

							   // cout<<"z1=="<<z1<<endl;
								//cout<<"r=="<<r<<endl;
								//cout<<"ans=="<<n/r<<endl;
				for (int i = 0; i < n1 / r; i++) {
					string a;
					int k2 = 0;
					for (int j = 0; j < n1 / r; j++) {
						k2 = ans[cnt++];
						//cout<<"k2=="<<k2<<endl;
						char temp = (char)(k2 - 1 + 'a');
						for (int f = 0; f < z1; f++)a += temp;
					}
					for (int w = 0; w < z1; w++) {
						cout << a << endl;
					}
				}
			}
		}
	}
	return 0;
}